package gui

import (
	"git-hawk/internal/logic/models"
	"log"
	"path/filepath"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/cmd/fyne_demo/data"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"github.com/go-git/go-git/v5"
)

func (ui *MainWindow) welcomeScreen(_ fyne.Window) fyne.CanvasObject {
	var welcomeLayout, createProjecLayout, addProjecLayout, cloneProjecLayout fyne.CanvasObject
	logo := canvas.NewImageFromResource(data.FyneLogoTransparent)
	logo.FillMode = canvas.ImageFillContain
	if fyne.CurrentDevice().IsMobile() {
		logo.SetMinSize(fyne.NewSize(192, 192))
	} else {
		logo.SetMinSize(fyne.NewSize(256, 256))
	}

	createBtn := widget.NewButton("创建项目", func() {})
	createBtn.OnTapped = func() {
		welcomeLayout.Hide() // 隐藏欢迎界面
		createProjecLayout.Show()
	}
	addBtn := widget.NewButton("添加项目", func() {})
	addBtn.OnTapped = func() {
		welcomeLayout.Hide() // 隐藏欢迎界面
		addProjecLayout.Show()

	}
	cloneBtn := widget.NewButton("克隆项目", func() {})
	cloneBtn.OnTapped = func() {
		welcomeLayout.Hide() // 隐藏欢迎界面
		cloneProjecLayout.Show()
		/* // 克隆仓库
		repo, err := git.PlainClone("/tmp/repo", false, &git.CloneOptions{
			URL:      "https://github.com/example/repo.git",
			Progress: os.Stdout, // 显示克隆进度
		})

		if err != nil {
			log.Fatalf("克隆失败: %v", err)
		}

		// 带认证的克隆
		_, err := git.PlainClone("/tmp/repo", false, &git.CloneOptions{
			URL: "https://github.com/private/repo",
			Auth: &http.BasicAuth{
				Username: "your-username", // 可以是任意非空字符串
				Password: "your-token",    // GitHub/GitLab 的个人访问令牌
			},
		}) */
	}

	welcomeLayout = container.NewCenter(container.NewVBox(
		widget.NewLabelWithStyle("欢迎使用 GitHawk", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
		logo,
		container.NewHBox(
			createBtn,
			widget.NewLabel("-"),
			addBtn,
			widget.NewLabel("-"),
			cloneBtn,
		),
		widget.NewLabel(""),
	))

	backBtn := widget.NewButton("返回", func() {})
	backBtn.OnTapped = func() {
		welcomeLayout.Show() // 显示欢迎界面
		createProjecLayout.Hide()
		addProjecLayout.Hide()
		cloneProjecLayout.Hide()
	}
	//============================= 创建项目 ================================
	nameRow1 := container.NewBorder(nil, nil, backBtn, nil, widget.NewLabel("创建一个仓库"))
	projectName := widget.NewLabel("项目名称:  ")
	projectNameInput := widget.NewEntry()
	projectNameInput.SetPlaceHolder("请输入项目名称")
	projectNameInput.OnChanged = func(content string) {

	}
	projectNameRow := container.NewBorder(nil, nil, projectName, nil, projectNameInput)
	projectPathName := widget.NewLabel("项目路径:  ")
	projectPathInput := widget.NewEntry()
	projectPathInput.SetPlaceHolder("请输入项目路径")
	browseBtn := widget.NewButton("浏览", func() {})
	browseBtn.OnTapped = func() {
		// 创建文件保存对话框
		saveDialog := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
			if err != nil {
				dialog.ShowError(err, ui.Window)
				return
			}
			if uri == nil {
				// 用户取消了操作
				return
			}
			// 添加当前选中路径
			path := uri.Path()

			projectPathInput.SetText(path)
		}, ui.Window)

		// 设置标题
		// saveDialog.SetTitleText("导入项目")

		saveDialog.SetDismissText("取消") // 可选，默认为"取消"
		// 设置确认按钮文本
		saveDialog.SetConfirmText("选择文件夹")
		saveDialog.Show()
	}
	projectPathNameRow := container.NewBorder(nil, nil, projectPathName, nil, projectPathInput)
	projectPathNameGridWrapLayout := container.New(layout.NewGridWrapLayout(fyne.Size{
		Width:  300,
		Height: 35,
	}), projectPathNameRow)
	projectPathNameBox := container.New(layout.NewHBoxLayout(), projectPathNameGridWrapLayout, browseBtn)

	createSaveBtn := widget.NewButton("创建", func() {})
	createSaveBtn.OnTapped = func() {
		// 初始化新的Git仓库
		_, err := ui.Services.InitRepository(projectNameInput.Text, projectPathInput.Text)
		if err != nil {
			log.Fatalf("初始化仓库失败: %v", err)
		}
		/* if ui.RepositoryList == nil {
			ui.RepositoryList = make(map[string]*RepositoryInfo)
		}
		//放到仓库列表中
		ui.RepositoryList[projectNameInput.Text] = &RepositoryInfo{Repo: repo}
		fmt.Println(projectNameInput.Text)
		fmt.Println(ui.RepositoryList[projectNameInput.Text]) */
		ui.RefreshTree()
	}
	mainCreateBtnRow := container.NewBorder(nil, nil, nil, nil, createSaveBtn)
	createProjecLayout = container.New(layout.NewVBoxLayout(), nameRow1, widget.NewLabel(""), projectNameRow, projectPathNameBox, widget.NewLabel(""), mainCreateBtnRow, widget.NewLabel(""))
	createProjecLayout.Hide()

	//============================= 添加项目 ================================
	addProjectLabel := container.NewBorder(nil, nil, backBtn, nil, widget.NewLabel("添加一个仓库"))
	addProjectPathName := widget.NewLabel("项目路径:  ")
	addProjectPathInput := widget.NewEntry()
	addProjectPathInput.SetPlaceHolder("请输入项目路径")
	addBrowseBtn := widget.NewButton("浏览", func() {})
	addBrowseBtn.OnTapped = func() {

		// 创建文件保存对话框
		saveDialog := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
			if err != nil {
				dialog.ShowError(err, ui.Window)
				return
			}
			if uri == nil {
				// 用户取消了操作
				return
			}
			// 添加当前选中路径
			path := uri.Path()
			//打开本地项目
			//repository *git.Repository
			repo, err := git.PlainOpen(path)
			if err != nil {
				log.Println("无法打开本地git项目:", err)
				log.Fatal(err)
			}

			projectParam := new(models.Project)
			//从路径中获取项目名称
			repoName := filepath.Base(path)
			projectParam.Name = repoName
			projectParam.Path = path
			ui.Services.SaveProject(projectParam)

			if ui.RepositoryList == nil {
				ui.RepositoryList = make(map[string]*RepositoryInfo)
			}
			//放到仓库列表中
			ui.RepositoryList[repoName] = &RepositoryInfo{Repo: repo}

			/* if t, ok := TreeTabData[t.Info.Name]; ok {
				setTreeTab(t)
			} */

		}, ui.Window)

		// 设置标题
		// saveDialog.SetTitleText("导入项目")

		saveDialog.SetDismissText("取消") // 可选，默认为"取消"
		// 设置确认按钮文本
		saveDialog.SetConfirmText("导入")
		saveDialog.Show()
	}
	addProjectPathNameRow := container.NewBorder(nil, nil, addProjectPathName, nil, addProjectPathInput)
	addProjectPathNameGridWrapLayout := container.New(layout.NewGridWrapLayout(fyne.Size{
		Width:  300,
		Height: 35,
	}), addProjectPathNameRow)
	addProjectPathNameBox := container.New(layout.NewHBoxLayout(), addProjectPathNameGridWrapLayout, addBrowseBtn)
	addProjectName := widget.NewLabel("项目名称:  ")
	addProjectNameInput := widget.NewEntry()
	addProjectNameInput.SetPlaceHolder("请输入项目名称")
	addProjectNameRow := container.NewBorder(nil, nil, addProjectName, nil, addProjectNameInput)

	addSaveBtn := widget.NewButton("添加", func() {})
	mainAddBtnRow := container.NewBorder(nil, nil, nil, nil, addSaveBtn)
	addProjecLayout = container.New(layout.NewVBoxLayout(), addProjectLabel, widget.NewLabel(""), addProjectPathNameBox, addProjectNameRow, widget.NewLabel(""), mainAddBtnRow, widget.NewLabel(""))
	addProjecLayout.Hide()

	//============================= 克隆项目 ================================
	cloneProjectLabel := container.NewBorder(nil, nil, backBtn, nil, widget.NewLabel("克隆一个仓库"))
	orgProjectPathName := widget.NewLabel("URL 路径:  ")
	orgProjectPathInput := widget.NewEntry()
	orgProjectPathInput.SetPlaceHolder("请输入URL路径")
	orgBrowseBtn := widget.NewButton("浏览", func() {})
	orgBrowseBtn.OnTapped = func() {
		// 创建文件保存对话框
		saveDialog := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
			if err != nil {
				dialog.ShowError(err, ui.Window)
				return
			}
			if uri == nil {
				// 用户取消了操作
				return
			}
			// 添加当前选中路径
			path := uri.Path()
			//项目名称
			projectNameInput.SetText(path)

		}, ui.Window)

		// 设置标题
		// saveDialog.SetTitleText("导入项目")

		saveDialog.SetDismissText("取消") // 可选，默认为"取消"
		// 设置确认按钮文本
		saveDialog.SetConfirmText("选择文件夹")
		saveDialog.Show()
	}
	orgProjectPathNameRow := container.NewBorder(nil, nil, orgProjectPathName, nil, orgProjectPathInput)
	orgProjectPathNameGridWrapLayout := container.New(layout.NewGridWrapLayout(fyne.Size{
		Width:  300,
		Height: 35,
	}), orgProjectPathNameRow)
	orgProjectPathNameBox := container.New(layout.NewHBoxLayout(), orgProjectPathNameGridWrapLayout, orgBrowseBtn)
	cloneProjectPathName := widget.NewLabel("本地路径:  ")
	cloneProjectPathInput := widget.NewEntry()
	cloneProjectPathInput.SetPlaceHolder("请输入本地路径")
	cloneBrowseBtn := widget.NewButton("浏览", func() {})
	cloneBrowseBtn.OnTapped = func() {
		// 创建文件保存对话框
		saveDialog := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
			if err != nil {
				dialog.ShowError(err, ui.Window)
				return
			}
			if uri == nil {
				// 用户取消了操作
				return
			}
			// 添加当前选中路径
			path := uri.Path()
			projectNameInput.SetText(path)

		}, ui.Window)

		// 设置标题
		// saveDialog.SetTitleText("导入项目")

		saveDialog.SetDismissText("取消") // 可选，默认为"取消"
		// 设置确认按钮文本
		saveDialog.SetConfirmText("选择文件夹")
		saveDialog.Show()
	}
	cloneProjectPathNameRow := container.NewBorder(nil, nil, cloneProjectPathName, nil, cloneProjectPathInput)
	cloneProjectPathNameGridWrapLayout := container.New(layout.NewGridWrapLayout(fyne.Size{
		Width:  300,
		Height: 35,
	}), cloneProjectPathNameRow)
	cloneProjectPathNameBox := container.New(layout.NewHBoxLayout(), cloneProjectPathNameGridWrapLayout, cloneBrowseBtn)
	cloneProjectName := widget.NewLabel("项目名称:  ")
	cloneProjectNameInput := widget.NewEntry()
	cloneProjectNameInput.SetPlaceHolder("请输入URL路径")
	cloneProjectNameRow := container.NewBorder(nil, nil, cloneProjectName, nil, cloneProjectNameInput)
	cloneSaveBtn := widget.NewButton("克隆", func() {})
	mainCloneBtnRow := container.NewBorder(nil, nil, nil, nil, cloneSaveBtn)
	// layout.NewSpacer()
	cloneProjecLayout = container.New(layout.NewVBoxLayout(), cloneProjectLabel, widget.NewLabel(""), orgProjectPathNameBox, cloneProjectPathNameBox, cloneProjectNameRow, widget.NewLabel(""), mainCloneBtnRow, widget.NewLabel(""), widget.NewLabel(""))
	cloneProjecLayout.Hide()

	return container.NewCenter(container.NewVBox(welcomeLayout, createProjecLayout, addProjecLayout, cloneProjecLayout))
}
